package web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import domain.News;
import domain.User;
import service.NewsService;

/**
 * 更新文章Servlet
 * @author XiaoBingBy
 *
 */
public class updateNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		News news = new News();
		try {
			BeanUtils.populate(news, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		User user = (User) request.getSession().getAttribute("myUser");
		news.setUserid(user.getUsername());
		news.setPubtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		/*添加到数据库*/
		NewsService ns = new NewsService();
		boolean updateNews = ns.updateNews(news);
		if (updateNews) {
			response.sendRedirect(request.getContextPath()+"/admin/news-pull.jsp");
		} else {
			System.out.println("发布错误");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
